package com.ca.ridersclubapplication.util;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import java.util.Timer;
import java.util.TimerTask;

public class KeyBoardUtil {

    public static void showSoftInput(final Context context, final View view) {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        view.setFocusable(true);
                        view.setFocusableInTouchMode(true);
                        //请求获得焦点
                        view.requestFocus();
                        //调用系统输入法
                        InputMethodManager inputManager = (InputMethodManager) view
                                .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputManager.showSoftInput(view, 0);
                    }
                });
            }
        }, 200);
    }

    public static void closeKeyBoard(Activity activity, View view) {

        //  使其失去焦点
        view.clearFocus();
        //  关闭软键盘
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
